package com.wingsfrog.travel.controller;

import com.wingsfrog.utils.wechat.WechatConst;
import com.wingsfrog.utils.wechat.WechatUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;

/**
 * Created by rankin on 16/4/9.
 */
@Controller
@RequestMapping(value = "wechat")
public class WechatController {
    Logger logger = Logger.getLogger(WechatController.class);
    @RequestMapping(value = "doScope", method = {RequestMethod.GET})
    public void doScope(@RequestParam String code, HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException {
        logger.error("code:"+code);
        Map m = WechatUtil.getUserInfoByCode(code);
        logger.error(m);
        response.sendRedirect(WechatConst.travelFront);
    }

    @RequestMapping(value = "login", method = {RequestMethod.GET})
    public void login(HttpServletResponse response) throws IOException {
        response.sendRedirect(WechatConst.createOauthUrl(WechatConst.redirectUri));
    }

}
